package com.tool.finances.lifestyle;

import android.app.Activity;
import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.tool.finances.lifestyle.data.AccountProvider;
import com.tool.finances.lifestyle.data.AccountType;

public class PasswordSetting extends Activity {

	public SharedPreferences preferences;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setTheme(android.R.style.Theme_Dialog);
		super.onCreate(savedInstanceState);
		setTitle("Password setting");

		setContentView(R.layout.setpassword);

		final CheckBox checkBox = (CheckBox) findViewById(R.id.CheckBox01IsHasPassword);

		final EditText oldpasswordText = (EditText) findViewById(R.id.EditText01OldPassword);
		final EditText passwordText = (EditText) findViewById(R.id.EditText01Password1);
		final EditText repasswordText = (EditText) findViewById(R.id.EditText02Password2);
		final Button setPasswordButton = (Button) findViewById(R.id.Button01SetPassword);
		Button cancelPasswordButton = (Button) findViewById(R.id.Button02Cancel);

		preferences = getSharedPreferences("setpassword", MODE_WORLD_WRITEABLE);
		int isHas = preferences.getInt("ishaspassword", 1);

		checkBox.setChecked(isHas == 0);

		if (!checkBox.isChecked()) {
			oldpasswordText.setEnabled(false);
			passwordText.setEnabled(false);
			repasswordText.setEnabled(false);
		}
		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				passwordText.setEnabled(isChecked);
				oldpasswordText.setEnabled(isChecked);
				repasswordText.setEnabled(isChecked);
			}
		});

		setPasswordButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				preferences = getSharedPreferences("setpassword",
						MODE_WORLD_WRITEABLE);
				SharedPreferences.Editor editor = preferences.edit();
				if (!checkBox.isChecked()) {
					editor.putInt("ishaspassword", 1);
					editor.commit();
					finish();
					return;
				}
				if (!oldpasswordText.getText().toString().equalsIgnoreCase("")
						&& !passwordText.getText().toString().equalsIgnoreCase(
								"")) {
					Cursor cursor = getContentResolver().query(
							AccountProvider.PASSWORD_URI,
							AccountType.Account.PASSWORDPROJECTION, null, null,
							null);
					if (cursor.moveToFirst()) {
						String password = cursor
								.getString(cursor
										.getColumnIndex(AccountType.Account.PASSWORDTYPE));
						if (password.equalsIgnoreCase(oldpasswordText.getText()
								.toString())) {
							if (passwordText
									.getText()
									.toString()
									.equalsIgnoreCase(
											repasswordText.getText().toString())) {
								ContentValues values = new ContentValues();
								values.put(AccountType.Account.PASSWORDTYPE,
										passwordText.getText().toString());
								int count = 0;
								try {
									count = getContentResolver().update(
											AccountProvider.PASSWORD_URI,
											values, " _id = 1 ", null);
								} catch (Exception e) {

								}

								if (count > 0) {
									Toast.makeText(PasswordSetting.this,
											"Update successfully", 1000).show();
									passwordText.setText("");
									repasswordText.setText("");

									editor.putInt("ishaspassword", 0);
									editor.commit();

									finish();
								}
							} else {
								passwordText.setText("");
								repasswordText.setText("");
								Toast
										.makeText(
												PasswordSetting.this,
												"Two password are different.Please reset.",
												1000).show();
							}
						} else {
							Toast.makeText(PasswordSetting.this,
									"Old password is wrong.", 1000).show();
						}
					}
				} else {
					Toast.makeText(PasswordSetting.this, "Input accurately",
							1000).show();
				}
			}
		});

		cancelPasswordButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

}
